package com.labManage.service.impl;

import com.labManage.dao.RoleDao;
import com.labManage.dao.RolePermitionDao;
import com.labManage.dao.UserRoleDao;
import com.labManage.entity.Role;
import com.labManage.entity.User;
import com.labManage.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class  RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RolePermitionDao rolePermitionDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Override
    public List<Role> getAllRoleLimit(Integer page, Integer limit,Role condition) {
        int startIndex = (page-1)*limit;
        List<Role> roles = roleDao.selectAllRoleLimit(startIndex, limit,condition);
        return roles;
    }

    @Override
    public int modifyRoleInfoById(Role role,List<Long> permitionIdsList) {
        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User)subject.getPrincipal();
        role.setModifyPerson(loginUser.getUsername());
        role.setCreateTime(new Date());
        role.setModifyTime(new Date());
        role.setCreatePerson(loginUser.getUsername());
        int result = roleDao.updateRoleInfoById(role);
        result = rolePermitionDao.deletePermitionByRoleId(role.getId());
        result = rolePermitionDao.updatePermitionByRoleId(role,permitionIdsList);
        return result;
    }

    @Override
    public List<Role> getAllRole() {
        List<Role> roles = roleDao.selectAllRole();
        return roles;
    }

    @Override
    public List<Role> getAllRoleByUserId(Long userId) {
        return roleDao.selectAllRoleByUserId(userId);
    }

    @Override
    public int addRoleInfo(Role role, List<Long> permitionIdsList) {
        Role selectRole = roleDao.selectRoleByRoleName(role.getRoleName());
        if(null == selectRole){
            role.setCreateTime(new Date());
            role.setModifyTime(new Date());
            Subject subject = SecurityUtils.getSubject();
            User loginUser = (User)subject.getPrincipal();
            role.setCreatePerson(loginUser.getUsername());
            role.setModifyPerson(loginUser.getUsername());
            int result = 0;
            result = roleDao.insertRole(role);
            /*role还没有ID*/
            Role insertRole = roleDao.selectRoleByRoleName(role.getRoleName());
            insertRole.setCreateTime(new Date());
            insertRole.setModifyTime(new Date());
            insertRole.setCreatePerson(loginUser.getUsername());
            insertRole.setModifyPerson(loginUser.getUsername());
            result = rolePermitionDao.insertRolePermition(insertRole,permitionIdsList);
            return result;
        }else{
            return -1;
        }
    }

    @Override
    public int deleteRoleInfo(Long roleId) {
        int result = 0;
        try{
            result = roleDao.deleteRoleByRoleId(roleId);
            result = userRoleDao.deleteUserRoleByRoleId(roleId);
            result = rolePermitionDao.deletePermitionByRoleId(roleId);
            return result;
        }catch (Exception e){
            return -1;
        }
    }
}
